{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "52820954",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "704e9aba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "customerID          0\n",
       "gender              0\n",
       "SeniorCitizen       0\n",
       "Partner             0\n",
       "Dependents          0\n",
       "tenure              0\n",
       "PhoneService        0\n",
       "MultipleLines       0\n",
       "InternetService     0\n",
       "OnlineSecurity      0\n",
       "OnlineBackup        0\n",
       "DeviceProtection    0\n",
       "TechSupport         0\n",
       "StreamingTV         0\n",
       "StreamingMovies     0\n",
       "Contract            0\n",
       "PaperlessBilling    0\n",
       "PaymentMethod       0\n",
       "MonthlyCharges      0\n",
       "TotalCharges        0\n",
       "Churn               0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 032\n",
    "df = pd.read_csv('./WA_Fn-UseC_-Telco-Customer-Churn.csv')\n",
    "df.head(3)\n",
    "df.isnull()\n",
    "df.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "df3c91da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 7043 entries, 0 to 7042\n",
      "Data columns (total 21 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   customerID        7043 non-null   object \n",
      " 1   gender            7043 non-null   object \n",
      " 2   SeniorCitizen     7043 non-null   int64  \n",
      " 3   Partner           7043 non-null   object \n",
      " 4   Dependents        7043 non-null   object \n",
      " 5   tenure            7043 non-null   int64  \n",
      " 6   PhoneService      7043 non-null   object \n",
      " 7   MultipleLines     7043 non-null   object \n",
      " 8   InternetService   7043 non-null   object \n",
      " 9   OnlineSecurity    7043 non-null   object \n",
      " 10  OnlineBackup      7043 non-null   object \n",
      " 11  DeviceProtection  7043 non-null   object \n",
      " 12  TechSupport       7043 non-null   object \n",
      " 13  StreamingTV       7043 non-null   object \n",
      " 14  StreamingMovies   7043 non-null   object \n",
      " 15  Contract          7043 non-null   object \n",
      " 16  PaperlessBilling  7043 non-null   object \n",
      " 17  PaymentMethod     7043 non-null   object \n",
      " 18  MonthlyCharges    7043 non-null   float64\n",
      " 19  TotalCharges      7043 non-null   object \n",
      " 20  Churn             7043 non-null   object \n",
      "dtypes: float64(1), int64(2), object(18)\n",
      "memory usage: 1.1+ MB\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "TotalCharges\n",
       "1397.475    11\n",
       "20.2        11\n",
       "19.75        9\n",
       "20.05        8\n",
       "19.9         8\n",
       "            ..\n",
       "6849.4       1\n",
       "692.35       1\n",
       "130.15       1\n",
       "3211.9       1\n",
       "6844.5       1\n",
       "Name: count, Length: 6531, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 033\n",
    "df.info()\n",
    "df['TotalCharges'].value_counts()\n",
    "median_value = df['TotalCharges'][df['TotalCharges'] != ' '].astype(float).median()\n",
    "df.loc[df['TotalCharges'] == ' ', 'TotalCharges'] = median_value\n",
    "df['TotalCharges'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe969f84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 7043 entries, 0 to 7042\n",
      "Data columns (total 21 columns):\n",
      " #   Column            Non-Null Count  Dtype   \n",
      "---  ------            --------------  -----   \n",
      " 0   customerID        7043 non-null   category\n",
      " 1   gender            7043 non-null   category\n",
      " 2   SeniorCitizen     7043 non-null   category\n",
      " 3   Partner           7043 non-null   category\n",
      " 4   Dependents        7043 non-null   category\n",
      " 5   tenure            7043 non-null   float64 \n",
      " 6   PhoneService      7043 non-null   category\n",
      " 7   MultipleLines     7043 non-null   category\n",
      " 8   InternetService   7043 non-null   category\n",
      " 9   OnlineSecurity    7043 non-null   category\n",
      " 10  OnlineBackup      7043 non-null   category\n",
      " 11  DeviceProtection  7043 non-null   category\n",
      " 12  TechSupport       7043 non-null   category\n",
      " 13  StreamingTV       7043 non-null   category\n",
      " 14  StreamingMovies   7043 non-null   category\n",
      " 15  Contract          7043 non-null   category\n",
      " 16  PaperlessBilling  7043 non-null   category\n",
      " 17  PaymentMethod     7043 non-null   category\n",
      " 18  MonthlyCharges    7043 non-null   float64 \n",
      " 19  TotalCharges      7043 non-null   float64 \n",
      " 20  Churn             7043 non-null   category\n",
      "dtypes: category(18), float64(3)\n",
      "memory usage: 611.1 KB\n"
     ]
    }
   ],
   "source": [
    "# 034\n",
    "df.columns\n",
    "number_columns = ['tenure', 'MonthlyCharges', 'TotalCharges']\n",
    "for column in number_columns:\n",
    "    df[column] = df[column].astype(float)\n",
    "for column in set(df.columns) - set(number_columns):\n",
    "    df[column] = df[column].astype('category')\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f7f411d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customerID</th>\n",
       "      <th>gender</th>\n",
       "      <th>SeniorCitizen</th>\n",
       "      <th>Partner</th>\n",
       "      <th>Dependents</th>\n",
       "      <th>PhoneService</th>\n",
       "      <th>MultipleLines</th>\n",
       "      <th>InternetService</th>\n",
       "      <th>OnlineSecurity</th>\n",
       "      <th>OnlineBackup</th>\n",
       "      <th>DeviceProtection</th>\n",
       "      <th>TechSupport</th>\n",
       "      <th>StreamingTV</th>\n",
       "      <th>StreamingMovies</th>\n",
       "      <th>Contract</th>\n",
       "      <th>PaperlessBilling</th>\n",
       "      <th>PaymentMethod</th>\n",
       "      <th>Churn</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "      <td>7043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>7043</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>0002-ORFBO</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>Yes</td>\n",
       "      <td>No</td>\n",
       "      <td>Fiber optic</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>Month-to-month</td>\n",
       "      <td>Yes</td>\n",
       "      <td>Electronic check</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>1</td>\n",
       "      <td>3555</td>\n",
       "      <td>5901</td>\n",
       "      <td>3641</td>\n",
       "      <td>4933</td>\n",
       "      <td>6361</td>\n",
       "      <td>3390</td>\n",
       "      <td>3096</td>\n",
       "      <td>3498</td>\n",
       "      <td>3088</td>\n",
       "      <td>3095</td>\n",
       "      <td>3473</td>\n",
       "      <td>2810</td>\n",
       "      <td>2785</td>\n",
       "      <td>3875</td>\n",
       "      <td>4171</td>\n",
       "      <td>2365</td>\n",
       "      <td>5174</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        customerID gender  SeniorCitizen Partner Dependents PhoneService  \\\n",
       "count         7043   7043           7043    7043       7043         7043   \n",
       "unique        7043      2              2       2          2            2   \n",
       "top     0002-ORFBO   Male              0      No         No          Yes   \n",
       "freq             1   3555           5901    3641       4933         6361   \n",
       "\n",
       "       MultipleLines InternetService OnlineSecurity OnlineBackup  \\\n",
       "count           7043            7043           7043         7043   \n",
       "unique             3               3              3            3   \n",
       "top               No     Fiber optic             No           No   \n",
       "freq            3390            3096           3498         3088   \n",
       "\n",
       "       DeviceProtection TechSupport StreamingTV StreamingMovies  \\\n",
       "count              7043        7043        7043            7043   \n",
       "unique                3           3           3               3   \n",
       "top                  No          No          No              No   \n",
       "freq               3095        3473        2810            2785   \n",
       "\n",
       "              Contract PaperlessBilling     PaymentMethod Churn  \n",
       "count             7043             7043              7043  7043  \n",
       "unique               3                2                 4     2  \n",
       "top     Month-to-month              Yes  Electronic check    No  \n",
       "freq              3875             4171              2365  5174  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 035\n",
    "df.describe(include=['category'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "826b21cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Churn\n",
       "No     5174\n",
       "Yes    1869\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 036\n",
    "df.head(3)\n",
    "df['Churn'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "09429c10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Churn  PaymentMethod            \n",
       "No     Bank transfer (automatic)    65.049417\n",
       "       Credit card (automatic)      64.562209\n",
       "       Electronic check             74.232032\n",
       "       Mailed check                 41.403911\n",
       "Yes    Bank transfer (automatic)    77.875581\n",
       "       Credit card (automatic)      77.356034\n",
       "       Electronic check             78.700980\n",
       "       Mailed check                 54.557143\n",
       "Name: MonthlyCharges, dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 037\n",
    "df.groupby(['Churn','PaymentMethod'], observed=True)['MonthlyCharges'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d7890366",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Churn\n",
       "0    5174\n",
       "1    1869\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 038\n",
    "df['Churn'].value_counts()\n",
    "df['Churn'] = df['Churn'].map({'Yes': 1, 'No': 0})\n",
    "df['Churn'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "93e2934f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 7043 entries, 0 to 7042\n",
      "Data columns (total 21 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   customerID        7043 non-null   object \n",
      " 1   gender            7043 non-null   object \n",
      " 2   SeniorCitizen     7043 non-null   int64  \n",
      " 3   Partner           7043 non-null   object \n",
      " 4   Dependents        7043 non-null   object \n",
      " 5   tenure            7043 non-null   int64  \n",
      " 6   PhoneService      7043 non-null   object \n",
      " 7   MultipleLines     7043 non-null   object \n",
      " 8   InternetService   7043 non-null   object \n",
      " 9   OnlineSecurity    7043 non-null   object \n",
      " 10  OnlineBackup      7043 non-null   object \n",
      " 11  DeviceProtection  7043 non-null   object \n",
      " 12  TechSupport       7043 non-null   object \n",
      " 13  StreamingTV       7043 non-null   object \n",
      " 14  StreamingMovies   7043 non-null   object \n",
      " 15  Contract          7043 non-null   object \n",
      " 16  PaperlessBilling  7043 non-null   object \n",
      " 17  PaymentMethod     7043 non-null   object \n",
      " 18  MonthlyCharges    7043 non-null   float64\n",
      " 19  TotalCharges      7043 non-null   object \n",
      " 20  Churn             7043 non-null   object \n",
      "dtypes: float64(1), int64(2), object(18)\n",
      "memory usage: 1.1+ MB\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeniorCitizen</th>\n",
       "      <th>tenure</th>\n",
       "      <th>MonthlyCharges</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>SeniorCitizen</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.016567</td>\n",
       "      <td>0.220173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tenure</th>\n",
       "      <td>0.016567</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.247900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MonthlyCharges</th>\n",
       "      <td>0.220173</td>\n",
       "      <td>0.247900</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                SeniorCitizen    tenure  MonthlyCharges\n",
       "SeniorCitizen        1.000000  0.016567        0.220173\n",
       "tenure               0.016567  1.000000        0.247900\n",
       "MonthlyCharges       0.220173  0.247900        1.000000"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 039\n",
    "df = pd.read_csv('./WA_Fn-UseC_-Telco-Customer-Churn.csv')\n",
    "median_value = df['TotalCharges'][df['TotalCharges'] != ' '].astype(float).median()\n",
    "df.loc[df['TotalCharges'] == ' ', 'TotalCharges'] = median_value\n",
    "df.info()\n",
    "df.corr(numeric_only=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e67ce9db",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 040\n",
    "df.sample(10).to_csv('./sample10.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
